<script setup>
import {ref, defineProps, watch, onMounted,computed} from 'vue'
import {getCompanyInfo, getJobInfo} from "@/views/messageBox/components/share.js";
import {messageStore} from "@/stores/modules/messageStore.js";

const props = defineProps({
  userInfo: Object,
  jobInfo: Object,
  time: String,
  content: String,
  unreadCount: Number
})

// 格式化时间
const nowRole = ref(messageStore().nowRole)
const formatTime = computed(() => {
  if (props.time) {
    return props.time.replace('T', ' ');
  }
  return props.time;
})

// 计算属性，判断 message是否以 "pdf" 结尾
const formattedContent = computed(() => {
  try {
    // 尝试解析 content 为 JSON
    const contentParsed = JSON.parse(props.content);

    // 如果解析成功，并且是一个有效的 JSON 格式
    if (contentParsed && typeof contentParsed === 'object') {
      // 检查是否以 ".pdf" 结尾
      if (contentParsed?.filePath.toLowerCase().endsWith('.pdf')) {
        return contentParsed.fileName;
      }
      return props.content
    }
  } catch (e) {
  }
  return props.content;
})

</script>

<template>
  <div class="message-card-container">
    <div class="avatar-container">
      <a-badge :count="unreadCount > 99 ? '99+' : unreadCount" offset="[10, -10]">
        <a-avatar :size="56" :src="nowRole === 'student'? userInfo?.logo:userInfo?.profilePicturePath "/>
      </a-badge>
    </div>
    <div class="info-container">
      <div class="above-container">
        <span class="name-box">
          <span class="name-text overflow-style">{{nowRole === 'student'? userInfo?.companyName: userInfo?.username}}</span>
          <i class="vline">|</i>
          <span class="job-title " v-if="nowRole==='student'">{{jobInfo?.title}}</span>
          <span class="job-title " v-if="nowRole==='company'">{{userInfo?.gender}}</span>
        </span>
        <div class="time-container">{{formatTime}}</div>
      </div>
      <div class="below-container overflow-style">
        <p>{{formattedContent}}</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.message-card-container {
  height: 60px;
  display: flex;
  align-items: center;
  margin: 5px;
  cursor: pointer;
}

.info-container {
  width: 235px;
  height: 90px;
  margin-left: 5px;
}

.job-title {
  max-width: 65px;
}

.avatar-container {
  display: flex;
  align-items: center;
  margin: 0 5px;
}

.above-container {
  text-align: center;
  align-items: center;
  display: flex;
  font-weight: 400;
  color: #333;
  line-height: 16px;
  margin-top: 20px;
}

.name-box {
  font-size: 13px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: inline-block;
  max-width: 120px;
}

.name-text {
  font-size: 15px;
  font-weight: 500;
  color: #222;
  margin-right: 10px;
  max-width: 50px;
}

.vline {
  margin: 0 4px;
  font-size: 16px;
  color: rgba(153, 153, 153, 0.57);
}

.time-container {
  position: absolute;
  right: 10px;
  font-size: 12px;
  color: rgba(153, 153, 153, 0.8);
  margin-right: 15px;
}

.below-container {
  margin-top: 15px;
  >p{
    font-size: 14px;
    color: rgba(153, 153, 153, 0.8);
    max-width: 235px;
  }
}

.overflow-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  transform: translateY(2px);
}
</style>
